package com.cxc.mp.simple.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.cxc.mp.simple.config.DatabaseConfig;
import org.springframework.beans.factory.annotation.Autowired;



@Component
public class OracleJDBCUtil {

    private static final String URL = "jdbc:oracle:thin:@10.10.180.237:1521:orcl";
    private static final String USERNAME = "ncc1909";
    private static final String PASSWORD = "ncc1909";
    static Logger LOGGER = Logger.getLogger(OracleJDBCUtil.class.getName());



    static {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e) {
            LOGGER.log(Level.SEVERE, "无法加载JDBC驱动", e);
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USERNAME, PASSWORD);
    }

    public static void closeConnection(Connection connection) {
        if (connection != null) {
            try {
                if (!connection.isClosed()) {
                    connection.close();
                }
            } catch (SQLException e) {
                LOGGER.log(Level.WARNING, "关闭数据库连接时发生错误", e);
            }
        }
    }
}
